package com.company.recruit.spider.service.serviceImpl;

import cn.hutool.http.HttpRequest;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;
import com.company.recruit.entity.ExcelData;
import com.company.recruit.entity.RecruitEntity;
import com.company.recruit.entity.RecruitMongo;
import com.company.recruit.service.RecruitService;
import com.company.recruit.spider.entity.BossData;
import com.company.recruit.spider.entity.JobsData;
import com.company.recruit.spider.service.SpiderService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author XiAoXu
 * @date 2021/12/8 12:18
 * @description 爬虫服务
 */
@Service
public class SpiderServiceImpl implements SpiderService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RecruitService recruitService;

    private static final String SPIDER_URL = "http://localhost:5000/initMessage";

    @Override
    public String pullData() {
        return HttpRequest.post(SPIDER_URL).execute().body();
    }

    @Override
    public void dealWithData() {
        List<BossData> boss = mongoTemplate.findAll(BossData.class, "boss");
        List<JobsData> jobs = mongoTemplate.findAll(JobsData.class, "jobs");
        // 根据地区合并
        List<BossData> allData = boss.stream().peek(item -> {
            for (JobsData jobsData : jobs) {
                //如果地区相同则拼接list
                if (Objects.equals(item.getArea(), jobsData.getArea())) {
                    item.getList().addAll(jobsData.getList());
                    break;
                }
            }
        }).collect(Collectors.toList());
        //mongoTemplate.insert(collect,"test");
        RecruitMongo recruitEntity = new RecruitMongo();
        //存入数据
        for (BossData allDatum : allData) {
            List<Object> list = allDatum.getList();
            for (Object o : list) {
                String s = JSON.toJSONString(o);
                ExcelData excelData = JSON.parseObject(s, ExcelData.class);

                recruitEntity.setWages(excelData.get薪资());
                recruitEntity.setDetailpage(excelData.get详情页());
                recruitEntity.setNature(excelData.get公司领域());
                recruitEntity.setScale(excelData.get公司规模());
                recruitEntity.setCompany(excelData.get公司名());
                recruitEntity.setPost(excelData.get标题());
                recruitEntity.setCity(excelData.get工作地点());
                recruitEntity.setEducation(excelData.get学历());
                recruitEntity.setExperience(excelData.get经验());
                recruitEntity.setWelfare(excelData.get福利());

                System.out.println(recruitEntity.toString());

                recruitService.saveRecruitEntity(recruitEntity);

            }
        }


        System.out.println("allData:"+allData.toString());



        //删除集合
        // mongoTemplate.dropCollection("boss");
        // mongoTemplate.dropCollection("jobs");



    }

}
